package com.sakura.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sakura.dto.InsertOrUpdateElderlyDTO;
import com.sakura.dto.QueryElderlyDTO;
import com.sakura.entity.Elderly;
import com.sakura.service.ElderlyService;
import com.sakura.util.EntityConverter;
import com.sakura.util.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : Sakura
 * @Date : 2023/11/22 16:11
 * @Description 老人模块
 */
@Api(value = "/api/elderly", tags = {"老人模块"})
@RestController
@RequestMapping("/api/elderly")
//解决跨域问题
@CrossOrigin
public class ElderlyController {
    @Autowired
    private ElderlyService elderlyService;


    /**
     * 根据名称查询管理员
     *
     * @param pageNo:当前页
     * @param pageSize:当前页大小
     * @param searchKey:查询关键字
     * @return R
     * @author Sakura
     * @date 2023/11/22 16:41
     */
    @ApiOperation(value = "查询当天需要检查但是还未检查的老人", notes = "", httpMethod = "GET")
    @GetMapping("/check/list/{pageNo}/{pageSize}")
    public R list(@ApiParam(value = "当前页码", required = true) @PathVariable Integer pageNo,
                  @ApiParam(value = "当前页码数据大小", required = true) @PathVariable Integer pageSize,
                  @ApiParam(value = "查询关键词", required = false) @RequestParam(required = false) String searchKey) {
        return R.success(elderlyService.getUnCheckElderlyPage(pageNo,pageSize,searchKey));
    }

    /**
     * 添加或者修改老人认证信息或者修改自己的信息
     *
     * @param elderlyDTO:
     * @return R
     * @author Sakura
     * @date 2023/11/22 17:26
     */
    @ApiOperation(value = "添加或者修改老人信息", notes = "添加或者修改老人信息", httpMethod = "POST")
    @PostMapping("/saveOrUpdate")
    public R addOrUpdate(@ApiParam(value = "添加或者修改管理员", type = "InsertOrUpdateManagerDTO", required = true) @RequestBody InsertOrUpdateElderlyDTO elderlyDTO) {
        boolean result = false;
        Elderly elderly = EntityConverter.copyAndGetSingle(elderlyDTO, Elderly.class);
        //等于null则是添加
        if (elderlyDTO.getId() == null || elderlyDTO.getId() == 0) {
            result = elderlyService.save(elderly);
            return R.getResult(result);
        }
        //不为null则是修改
        result = elderlyService.updateById(elderly);
        return R.getResult(result);

    }

    /**
     * 根据id查找老人
     *
     * @param id:
     * @return R
     * @author Sakura
     * @date 2023/11/22 19:06
     */
    @ApiOperation(value = "根据id查找老人", notes = "根据id查找老人", httpMethod = "GET")
    @GetMapping("/query/{id}")
    public R queryOne(@ApiParam(value = "网格员id", required = true) @PathVariable Long id) {
        return R.success(elderlyService.getById(id));
    }


    /**
     * 根据id删除老人
     *
     * @param id:
     * @return R
     * @author Sakura
     * @date 2023/11/22 19:06
     */
    @ApiOperation(value = "根据id删除老人", notes = "根据id删除老人")
    @DeleteMapping("/delete/{id}")
    public R delete(@ApiParam(value = "老人id", required = true) @PathVariable Long id) {
        return R.getResult(elderlyService.removeById(id));
    }


    /**
     * 根据phone查找老人
     *
     * @param phone:
     * @return R
     * @author Sakura
     * @date 2023/11/22 19:06
     */
    @ApiOperation(value = "根据phone查找老人", notes = "根据phone查找老人", httpMethod = "GET")
    @GetMapping("/phone/{phone}")
    public R queryOneByPhone(@ApiParam(value = "老人电话", required = true) @PathVariable String phone) {
        return R.success(elderlyService.getElderlyByPhone(phone));
    }


    /**
     * 添加或者修改老人认证信息或者修改自己的信息
     *
     * @param elderlyDTO:
     * @return R
     * @author Sakura
     * @date 2023/11/22 17:26
     */
    @ApiOperation(value = "绑定老人信息", notes = "绑定老人信息", httpMethod = "POST")
    @PostMapping("/binding")
    public R binding(@ApiParam(value = "绑定老人信息", type = "InsertOrUpdateManagerDTO", required = true) @RequestBody InsertOrUpdateElderlyDTO elderlyDTO) {
       //首先需要判断数据库是否有这个用户
        Elderly elderlyDB = elderlyService.getElderlyByPhone(elderlyDTO.getPhone());

        if (elderlyDB == null) {
            //未注册则添加信息
            Elderly elderly = EntityConverter.copyAndGetSingle(elderlyDTO, Elderly.class);
            elderly.setId(null);
            elderlyService.save(elderly);
            Map<String, Object> map = new HashMap<>();
            map.put("msg", "绑定成功");
            LambdaQueryWrapper<Elderly> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Elderly::getPhone, elderlyDTO.getPhone());
            elderlyDB = elderlyService.getOne(wrapper);

            map.put("user",elderlyDB);
            return R.success(map);
        }
        //已经注册就是修改信息

        // 这里有一个bug，我是根据电话号码来确定id，但是电话号码可以修改，一旦一个用户修改了电话号码，那么这个用户的id就会改变，
        // 我们通过id去查找以前记录就会有逻辑问题，所以这里最好的结果就是将新电话号码和旧电话号码都要传递过来，然后根据旧电话号码查询人员具体id，
        // 然后修改id，然后修改其他信息
        // elderlyDB.setName(elderlyDTO.getName()).setIdCard(elderlyDTO.getIdCard());
        //boolean result = elderlyService.updateById(elderlyDB);
        //所以我们修改逻辑
//        elderlyService.updateById()
        Map<String, Object> map = new HashMap<>();
        map.put("msg", "修改成功");
        Elderly getSingle = EntityConverter.copyAndGetSingle(elderlyDB, Elderly.class);
        elderlyService.updateById(getSingle);
        map.put("user",getSingle);
        return R.success(map);
    }

    @ApiOperation(value = "根据id查找老人", notes = "根据id查找老人")
    @PostMapping("/list")
    public R list(@ApiParam(value = "网格员id",type = "QueryElderlyVO") @RequestBody QueryElderlyDTO queryElderlyDTO ) {
        return elderlyService.getPage(queryElderlyDTO);
    }

    @ApiOperation(value = "根据集合删除老人", notes = "根据集合删除老人")
    @DeleteMapping("/batch")
    public R delete(@ApiParam(value = "项老人合id", required = true) @RequestBody List<Long> ids) {
        if (ids.size() == 0) {
            return R.error("请选择要删除的老人");
        }
        boolean result = elderlyService.removeByIds(ids);
        return R.getResult(result);
    }

}
